package com.aop;

import java.util.Arrays;

import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class LoggerAop {

	Logger log = Logger.getLogger(LoggerAop.class);
	
	@Pointcut("execution(* com.service.impl.*.*(..))")
	public void pc(){}
	
	@Around("pc()")
	public Object aound(ProceedingJoinPoint jp){
		Object result = null;
		String classname = jp.getTarget().getClass().getSimpleName();
		String methodname = jp.getSignature().getName();
		Object[] args = jp.getArgs();
		try {
			log.info("调用【"+classname+"】类的【"+methodname+"】方法，传递参数为："+Arrays.toString(args));
			result = jp.proceed();
			log.info("调用【"+classname+"】类的【"+methodname+"】方法，返回结果为："+result);
		} catch (Throwable e) {
			e.printStackTrace();
			log.error("调用【"+classname+"】类的【"+methodname+"】方法，出现异常，异常信息为："+e.getMessage());
		}
		return result;
	}
}
